package com.yingxue.demo.config;

import com.yingxue.demo.constant.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @Package: com.company.scaffold.config
 * @Author: Mr.Waves
 * @Date: 2020-03-08 12:18
 * @Description:
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${swagger2.enable}")
    private boolean enable;

    @Bean
    public Docket createRestApi() {
        ArrayList<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        ParameterBuilder refreshTokenPar = new ParameterBuilder();
        tokenPar.name(Constant.ACCESS_TOKEN).description("swagger测试用（模拟authorization传入）非必填header")
                .modelRef(new ModelRef("string")).parameterType("header").required(false);
        refreshTokenPar.name(Constant.REFRESH_TOKEN).description("swagger测试用（模拟刷新token传入）非必填header")
                .modelRef(new ModelRef("string")).parameterType("header").required(false);

        pars.add(tokenPar.build());
        pars.add(refreshTokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.company.scaffold.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .enable(enable);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Java项目脚手架")
                .description("SpringBoot2+Shiro+Redis+MybatisPlus+Mariadb+JWT+Swagger2")
                .termsOfServiceUrl("")
                .version("0.0.1")
                .build();
    }
}
